package com.kelan.android.app.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kelan.android.app.entity.AjaxResult;
import com.kelan.android.app.entity.DrListEntity;
import com.kelan.android.app.entity.EasyUIDataGridResult;
import com.kelan.android.app.service.DrListService;

import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "双随机检查")
@RestController
@RequestMapping("/dr")
public class DrListController {

	@Autowired
	private DrListService drService;
	
	/**
	 * @Title: getStrList
	 * @Description:  根据用户信息获取双随机检查信息列表
	 * @Author 刘仁
	 * @DateTime 2020年3月25日 下午5:00:45
	 * @param pageNum
	 * @param rows
	 * @param drListEntity
	 * @return
	 */
	@ApiOperation(value = "双随机检查列表", nickname = "双随机检查列表", notes = "入参是对象")
	@GetMapping("/list")
	@ResponseBody
	public EasyUIDataGridResult getStrList(@RequestParam(value="page",required = true, defaultValue = "1") Integer pageNum ,
            @RequestParam(value="rows",required = true, defaultValue = "10") Integer rows, DrListEntity drListEntity, HttpServletRequest req){
		PageHelper.startPage(pageNum , rows);
		HttpSession session = req.getSession();
		String inspectId = String.valueOf(session.getAttribute("inspectId"));
		System.out.println("deptCode=======>" + String.valueOf(session.getAttribute("deptCode")));
		if (!inspectId.equals("hkadmin")&&(!StringUtils.isBlank(inspectId)&&!"null".equals(inspectId))) {
			drListEntity.setCreaterDeptId(String.valueOf(session.getAttribute("deptCode")));
		}
		List<DrListEntity> list = drService.selectDrList(drListEntity);
		PageInfo<DrListEntity> personPageInfo = new PageInfo<>(list);
		EasyUIDataGridResult dataGridResult = new EasyUIDataGridResult();
		dataGridResult.setTotal(Integer.parseInt(personPageInfo.getTotal()+""));
		dataGridResult.setRows(list);
		return dataGridResult;
	}
	
	/**
	 * @Title: getStrList
	 * @Description:  根据用户信息获取双随机检查信息列表
	 * @Author 刘仁
	 * @DateTime 2020年3月25日 下午5:00:45
	 * @param pageNum
	 * @param rows
	 * @param drListEntity
	 * @return
	 */
	@ApiOperation(value = "根据id查询数据详细信息", nickname = "获取检查详细信息", notes = "入参是id")
	@GetMapping("/{id}")
	@ResponseBody
	public AjaxResult getDrById(@PathVariable("id") String id){
		DrListEntity drListEntity = drService.selectDrById(id);
		return AjaxResult.success(drListEntity);
	}
	
	/**
	 * @Title: updateDr
	 * @Description: 更新检查结果
	 * @Author 刘仁
	 * @DateTime 2020年3月25日 下午5:13:11
	 * @param drListEntity
	 * @return
	 */
	@ApiOperation(value = "录入双随机检查结果", nickname = "录入双随机检查结果", notes = "入参是对象")
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult updateDr(HttpServletRequest req,DrListEntity drListEntity) {
		HttpSession session = req.getSession();
		String inspectId = String.valueOf(session.getAttribute("inspectId"));
		System.out.println("=========inspectId=====>>>" + inspectId  );
		if (StringUtils.isBlank(inspectId)) {
			System.out.println("==============>>>用户未登录不能修改数据!"  );
			return AjaxResult.error("用户未登录不能修改数据!");
		}
		if (StringUtils.isBlank(drListEntity.getId())) {
			System.out.println("==============>>>主键ID不能为空!"  );
			return AjaxResult.error("主键ID不能为空!");
		}
		
		if (StringUtils.isBlank(drListEntity.getCheckActionName())) {
			System.out.println("==============>>>检查行为名称不能为空!"  );
			return AjaxResult.error("检查行为名称不能为空!");
		}
		
		if (StringUtils.isBlank(drListEntity.getCheckActionCode())) {
			System.out.println("==============>>>检查行为编号不能为空!"  );
			return AjaxResult.error("检查行为编号不能为空!");
		}
		
		if (StringUtils.isBlank(drListEntity.getCheckForm())) {
			System.out.println("==============>>>检查形式不能为空!"  );
			return AjaxResult.error("检查形式不能为空!");
		}
		
		if (StringUtils.isBlank(drListEntity.getCheckType())) {
			System.out.println("==============>>>检查类别不能为空!"  );
			return AjaxResult.error("检查类别不能为空!");
		}
		
		if (StringUtils.isBlank(drListEntity.getCheckMode())) {
			System.out.println("==============>>>检查方式不能为空!"  );
			return AjaxResult.error("检查方式不能为空!");
		}
		
		if (StringUtils.isBlank(drListEntity.getResult())) {
			System.out.println("==============>>>检查结果不能为空!"  );
			return AjaxResult.error("检查结果不能为空!");
		}
		
		if (StringUtils.isBlank(drListEntity.getInspectDate())) {
			System.out.println("==============>>>检查时间不能为空!"  );
			return AjaxResult.error("检查时间不能为空!");
		}
		
		if (StringUtils.isBlank(drListEntity.getHasEntrust())) {
			System.out.println("==============>>>是否委托实施不能为空!"  );
			return AjaxResult.error("是否委托实施不能为空!");
		}
		DrListEntity drListEntityXX = drService.selectDrById(drListEntity.getId());
		if (drListEntityXX ==null) {
			System.out.println("==============>>>检查信息不存在!"  );
			return AjaxResult.error("检查信息不存在!");
		}
		if ("1".equals(drListEntityXX.getHasBack())) {
			System.out.println("==============>>>已经反馈过!"  );
			return AjaxResult.error("已经反馈过!");
		}
		
		drListEntity.setBackerId(String.valueOf(session.getAttribute("inspectId")));
		drListEntity.setBackerName(String.valueOf(session.getAttribute("inspectName")));
		drListEntity.setBackDate(DateUtil.format(new Date(), "yyyy/MM/dd HH:mm:ss"));
		drListEntity.setDataSource("1");
		drListEntity.setHasBack("1");//是否反馈 0：否 1 ：是

		return AjaxResult.success(drService.editDr(drListEntity));
	}
}
